package sample.stock;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class EODData extends Activity{
	private Spinner exchange = null;
	private AutoCompleteTextView symbol = null;
	private Button check = null;

	private String ex = "AMEX";
	
	private ProgressDialog progressDialog;
	
	private ArrayAdapter adapterAMEX = null;
    private ArrayAdapter adapterASX = null;
    private ArrayAdapter adapterCBOT = null;
    private ArrayAdapter adapterCFE = null;
    private ArrayAdapter adapterCME = null;
    private ArrayAdapter adapterCOMEX = null;
    private ArrayAdapter adapterFOREX = null;
    private ArrayAdapter adapterHKEX = null;
    private ArrayAdapter adapterINDEX = null;
    private ArrayAdapter adapterKCBT = null;
    private ArrayAdapter adapterLIFFE = null;
    private ArrayAdapter adapterLSE = null;
    private ArrayAdapter adapterMGEX = null;
    private ArrayAdapter adapterMLSE = null;
    private ArrayAdapter adapterNASDAQ = null;
    private ArrayAdapter adapterNYBOT = null;
    private ArrayAdapter adapterNYMEX = null;
    private ArrayAdapter adapterNYSE = null;
    private ArrayAdapter adapterOPRA = null;
    private ArrayAdapter adapterOTCBB = null;
	
	@SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.eod);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        exchange = (Spinner)findViewById(R.id.spinnerExchange);
        symbol = (AutoCompleteTextView)findViewById(R.id.autoCompleteSymbol);
        check = (Button)findViewById(R.id.check);
        symbol.setThreshold(0); // set number of character input to populate list
        
        // set default
        adapterAMEX = ArrayAdapter.createFromResource(EODData.this, R.array.AMEX, R.layout.list_ticker);
        symbol.setAdapter(adapterAMEX);
        
        progressDialog = new ProgressDialog(EODData.this);
		progressDialog.setMessage("Loading...");
		new LoadDataTask(progressDialog).execute();
        
        final ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.exchange, android.R.layout.simple_spinner_item);
        
        exchange.setAdapter(adapter);
        exchange.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(exchange.getSelectedItemPosition()){
				case 0: default:
					ex = "AMEX";
					symbol.setAdapter(adapterAMEX);
					break;
				case 1:
					ex = "ASX";
					symbol.setAdapter(adapterASX);
					break;
				case 2:
					ex = "CBOT";
					symbol.setAdapter(adapterCBOT);
					break;
				case 3:
					ex = "CFE";
					symbol.setAdapter(adapterCFE);
					break;
				case 4:
					ex = "CME";
					symbol.setAdapter(adapterCME);
					break;
				case 5:
					ex = "COMEX";
					symbol.setAdapter(adapterCOMEX);
					break;
				case 6:
					ex = "FOREX";
					symbol.setAdapter(adapterFOREX);
					break;
				case 7:
					ex = "HKEX";
					symbol.setAdapter(adapterHKEX);
					break;
				case 8:
					ex = "INDEX";
					symbol.setAdapter(adapterINDEX);
					break;
				case 9:
					ex = "KBCT";
					symbol.setAdapter(adapterKCBT);
					break;
				case 10:
					ex = "LIFFE";
					symbol.setAdapter(adapterLIFFE);
					break;
				case 11:
					ex = "LSE";
					symbol.setAdapter(adapterLSE);
					break;
				case 12:
					ex = "MGEX";
					symbol.setAdapter(adapterMGEX);
					break;
				case 13:
					ex = "MLSE";
					symbol.setAdapter(adapterMLSE);
					break;
				case 14:
					ex = "NASDAQ";
					symbol.setAdapter(adapterNASDAQ);
					break;
				case 15:
					ex = "NYBOT";
					symbol.setAdapter(adapterNYBOT);
					break;
				case 16:
					ex = "NYMEX";
					symbol.setAdapter(adapterNYMEX);
					break;
				case 17:
					ex = "NYSE";
					symbol.setAdapter(adapterNYSE);
					break;
				case 18:
					ex = "OPRA";
					symbol.setAdapter(adapterOPRA);
					break;
				case 19:
					ex = "OTBB";
					symbol.setAdapter(adapterOTCBB);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        check.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent chartView = new Intent(EODData.this, EODChart.class);
				chartView.putExtra("symbol", symbol.getText().toString());
				chartView.putExtra("exchange",ex);
				startActivity(chartView);
			}
		});
    }
	
	private class LoadDataTask  extends AsyncTask<Void, Void, Void> {
	  	  private ProgressDialog progress;
	  	  
	  	  public LoadDataTask(ProgressDialog progress) {
	  		    this.progress = progress;
	  		  }
	
	  		  public void onPreExecute() {
	  		    progress.show();
	  		  }
	
	  		  public void onPostExecute(Void unused) {
	  			  progress.dismiss();
	  		  }
	
			@SuppressWarnings("deprecation")
			@Override
	  		protected Void doInBackground(Void... arg0) {		
		         adapterASX = ArrayAdapter.createFromResource(EODData.this, R.array.ASX, R.layout.list_ticker);
		         adapterCBOT = ArrayAdapter.createFromResource(EODData.this, R.array.CBOT, R.layout.list_ticker);
		         adapterCFE = ArrayAdapter.createFromResource(EODData.this, R.array.CFE, R.layout.list_ticker);
		         adapterCME = ArrayAdapter.createFromResource(EODData.this, R.array.CME, R.layout.list_ticker);
		         adapterCOMEX = ArrayAdapter.createFromResource(EODData.this, R.array.COMEX, R.layout.list_ticker);
		         adapterFOREX = ArrayAdapter.createFromResource(EODData.this, R.array.FOREX, R.layout.list_ticker);
		         adapterHKEX = ArrayAdapter.createFromResource(EODData.this, R.array.HKEX, R.layout.list_ticker);
		         adapterINDEX = ArrayAdapter.createFromResource(EODData.this, R.array.INDEX, R.layout.list_ticker);
		         adapterKCBT = ArrayAdapter.createFromResource(EODData.this, R.array.KCBT, R.layout.list_ticker);
		         adapterLIFFE = ArrayAdapter.createFromResource(EODData.this, R.array.LIFFE, R.layout.list_ticker);
		         adapterLSE = ArrayAdapter.createFromResource(EODData.this, R.array.LSE, R.layout.list_ticker);
		         adapterMGEX = ArrayAdapter.createFromResource(EODData.this, R.array.MGEX, R.layout.list_ticker);
		         adapterMLSE = ArrayAdapter.createFromResource(EODData.this, R.array.MLSE, R.layout.list_ticker);
		         adapterNASDAQ = ArrayAdapter.createFromResource(EODData.this, R.array.NASDAQ, R.layout.list_ticker);
		         adapterNYBOT = ArrayAdapter.createFromResource(EODData.this, R.array.NYBOT, R.layout.list_ticker);
		         adapterNYMEX = ArrayAdapter.createFromResource(EODData.this, R.array.NYMEX, R.layout.list_ticker);
		         adapterNYSE = ArrayAdapter.createFromResource(EODData.this, R.array.NYSE, R.layout.list_ticker);
		         adapterOPRA = ArrayAdapter.createFromResource(EODData.this, R.array.OPRA, R.layout.list_ticker);
		         adapterOTCBB = ArrayAdapter.createFromResource(EODData.this, R.array.ASX, R.layout.list_ticker);
		         return null;
	  		}
  }
}
